package com.example.demo.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.demo.modules.entity.DeviceInfo;
import com.example.demo.modules.mapper.DeviceInfoMapper;
import com.example.demo.modules.service.IDeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengyingya
 * @since 2023-04-12
 */
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService {
    @Override
    public boolean saveDeviceInfo(DeviceInfo deviceInfo) {

        //如果表中存在这个设备的数据，就更新字段，如果不存在，就添加一条新数据
        // 先通过device_name从表中查询，看是否存在这个设备
        if(getOne(new QueryWrapper<DeviceInfo>().eq("device_name", deviceInfo.getDeviceName())) == null){
            return save(deviceInfo);
        } else {

            QueryWrapper<DeviceInfo> queryWrapper =
                    new QueryWrapper<DeviceInfo>().eq("device_name", deviceInfo.getDeviceName());
            return update(deviceInfo, queryWrapper);
        }
    }
}
